/**
 *  Extends the prototype object with one or more objects.
 */
function extend_class() {
    var args = Array.prototype.slice.call(arguments);
    var Class = args.shift(), BaseClass = null;
    while ((BaseClass = args.shift())) {
        for (var member in BaseClass) {
            Class.prototype[member] = BaseClass[member];
        }
    }
}

var EntityBase = {
    EntityBase_init : function(x, y, width, height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    },
    getWidth : function() {
        return this.width;
    },
    getHeight : function() {
        return this.height;
    },
    getX : function() {
        return this.x;
    },
    getY : function() {
        return this.y;
    },
    draw : function(context) {
        var red = parseInt(Math.random() * 255);
        var green = parseInt(Math.random() * 255);
        var blue = parseInt(Math.random() * 255);

        context.fillStyle = 'rgb(' + red + ',' + green + ',' + blue + ')';
        context.fillRect(this.x, this.y, this.width, this.height);
    },
    ontick : function(t, dt) {},
    collidesWith : function(x, y) {
        if (x > this.x && x < this.x + this.width) {
            if (y > this.y && y < this.y + this.height) {
                return true;
            }
        }
        return false;
    }
};

function BackgroundEntity(canvas) {
    var self = this;
    this.canvas = canvas;
    this.particles = [];
}

extend_class(BackgroundEntity, EntityBase);

BackgroundEntity.prototype.draw = function(context) {
    context.fillStyle = 'rgb(255, 255, 255)';
    for(particleIdx in this.particles) {
        var particle = this.particles[particleIdx];
        context.fillRect(particle.x, particle.y, 1, 1);
    }
};

BackgroundEntity.prototype.ontick = function(t, dt) {
    if(Math.random() > 0.5) {
        this.particles.push({x:Math.random()*this.canvas.width, y:0, sx:0, sy:1});
    }

    for(particleIdx in this.particles) {
        var particle = this.particles[particleIdx];
        particle.y += particle.sy;
        particle.x += particle.sx;
        particle.sx += (-1 + Math.random()*2)*0.7;
        //particle.sy = 1 + (-1 + Math.random()*2)*0.5;
        //particle.sy += 0.1;
        if(particle.y > this.canvas.height) {
            //console.info(this.particles.length);
            this.particles.splice(this.particles.indexOf(particle), 1);
        }
    }

};

function Box(life) {
    var self = this;
    this.clickCount = 0;
    this.life = life;
    this.clicked = function() {
        self.clickCount++;
    }
}

extend_class(Box, EntityBase);

Box.prototype.draw = function(context) {
    var alpha = 1-(this.clickCount/this.life);
    context.fillStyle = 'rgba(32,32,128, ' + alpha + ')';
    context.fillRect(this.x, this.y, this.width, this.height);
    context.fillStyle = 'rgba(255, 255, 255, ' + alpha + ')';
    context.textBaseline = "top";
    context.font = "bold 14px sans-serif";
    context.fillText(this.life - this.clickCount, this.x, this.y);
};

function Door() {
    var self = this;
    this.isOpen = false;
    this.setOpen = function(isOpen) {
        self.isOpen = isOpen;
    }
}

extend_class(Door, EntityBase);

Door.prototype.draw = function(context, backSide) {
    context.fillStyle = backSide ? 'rgb(0,0,0)' : (this.isOpen ? 'rgb(0,255,0)' : 'rgb(255,0,0)');
    context.fillRect(this.x, this.y, this.width, this.height);
};

function Explosion(targetEntity) {
    var self = this;
    this.targetEntity = targetEntity;

    this.particles = [];
    for(var i = 0; i < 10; i++) {
        this.particles.push({x: 0, y: 0, w: 0, h: 0, c: 0, direction: 0, speed: 0});
    }

}

extend_class(Explosion, EntityBase);